package edu.guet.server.controller;

import edu.guet.server.model.Stu;
import edu.guet.server.model.dto.ShowSemesterDTO;
import edu.guet.server.model.dto.StuSemDTO;
import edu.guet.server.model.dto.StudentScoreDTO;
import edu.guet.server.service.SemesterService;
import edu.guet.server.service.StuSemService;
import edu.guet.server.utils.CompareCurTime;
import edu.guet.server.utils.DatetimeSwitch;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/semesters")
public class StudentSemesterController {
    @Autowired
    StuSemService stuSemService;
    @Autowired
    SemesterService semesterService;

    /**
     * 展示学生个人中心(全部)
     *
     * @param user_id
     * @return
     */
    @RequestMapping("/showstulist/All")
    ResultAPI showStulist(Integer user_id) {
        if (user_id == null) {
            return ResultAPI.error("学生id为空");
        }
        List<ShowSemesterDTO> showList;
        log.info("学生id：{}", user_id);
        //获得学生关联的课程的所有id
        List<Integer> semIds = stuSemService.findsemesterIdsByuserId(user_id);
        //批量搜一步到位
        log.info("学生中心（全部）找到的课程id{}", semIds.toString());
        showList = semesterService.findSemesterByIds(semIds, null);
        for (ShowSemesterDTO semItem : showList) {
            //判断是否开始
            if (!CompareCurTime.compareTime(semItem.getStart_time())) {
                //开始课程
                semesterService.updateSemStatus(semItem.getSemester_id(), 1);
                semItem.setSemester_status(1);
            }
            //判断是否过期
            if (!CompareCurTime.compareTime(semItem.getEnd_time())) {
                //关闭课程
                semesterService.updateSemStatus(semItem.getSemester_id(), 0);
                semItem.setSemester_status(0);
            }
        }
        return ResultAPI.success(showList);
    }
//    /**
//     * 展示学生个人中心(进行中)
//     * @param user_id
//     * @return
//     */
//    @RequestMapping("/showstulist/Ongoing")
//    ResultAPI showStulistOngoing(Integer user_id) {
//        if (user_id == null) {
//            return ResultAPI.error("学生id为空");
//        }
//        List<ShowSemesterDTO> showList;
//        log.info("学生id：{}",user_id);
//        //获得学生关联的课程的所有id
//        List<Integer> semIds = stuSemService.findsemesterIdsByuserId(user_id);
//        //批量搜一步到位
//        log.info("学生中心（全部）找到的课程id{}",semIds.toString());
//        showList=semesterService.findSemesterByIds(semIds,1);
//        for(ShowSemesterDTO semItem:showList){
//            //判断是否过期
//            if(!CompareCurTime.compareTime(semItem.getEnd_time())){
//                //关闭课程
//                semesterService.updateSemStatus(semItem.getCourse_id(),0);
//                semItem.setSmester_status(0);
//            }
//        }
//        return ResultAPI.success(showList);
//    }
//    /**
//     * 展示学生个人中心(已结束)
//     * @param user_id
//     * @return
//     */
//    @RequestMapping("/showstulist/Closed")
//    ResultAPI showStulistClosed(Integer user_id) {
//        if (user_id == null) {
//            return ResultAPI.error("学生id为空");
//        }
//        List<ShowSemesterDTO> showList;
//        log.info("学生id：{}",user_id);
//        //获得学生关联的课程的所有id
//        List<Integer> semIds = stuSemService.findsemesterIdsByuserId(user_id);
//        //批量搜一步到位
//        log.info("学生中心（全部）找到的课程id{}",semIds.toString());
//        showList=semesterService.findSemesterByIds(semIds,0);
//        return ResultAPI.success(showList);
//    }

    /**
     * 老师导入系统批量学生选课
     *
     * @return
     */
    @Transactional
    @PostMapping("/addstusem")
    ResultAPI addStuSem(@RequestBody List<Stu> stus, @RequestParam Integer semester_id) {
        log.info("加课数据：{}", stus);
        return stuSemService.addStuSem(stus,semester_id);
    }

    /**
     * 展示学生所有成绩
     * @param semester_id
     * @return
     */
    @GetMapping("/showStudentsScore")
    ResultAPI showStudentsScore(@RequestParam Integer semester_id) {
        List<StudentScoreDTO> list = stuSemService.findStudentScores(semester_id);
        return ResultAPI.success(list);
    }

    /**
     * 展示学号姓名
     * @param semester_id
     * @return
     */
    @GetMapping("/showStudents")
    ResultAPI showStudents(@RequestParam Integer semester_id) {
        List<Stu> stus= stuSemService.getStuList(semester_id);
        return ResultAPI.success(stus);
    }

    @PutMapping("/editGrade")
    ResultAPI editGrade(@RequestBody StudentScoreDTO editData) {
        return stuSemService.editStuScore(editData);
    }
}
